import { ref } from 'vue'

/**
 * BaseTable组件筛选框处理hook
 * @param dataTableRef BaseTable的ref对象
 * @returns {*}
 */
export function useSelection(dataTableRef: any, key = 'id') {
  // 已选择项目
  const selection = ref<any[]>([])
  // 当前选择项目
  const curSelection = ref<any[]>([])
  // 暂存当前页初始选择项目
  const tmpSelection = ref<any[]>([])
  // 已选项目备份
  const selectionBac = ref<any[]>([])
  // 当前选择项目备份
  const curSelectionBac = ref<any[]>([])
  // 暂存当前页初始选择项目备份
  const tmpSelectionBac = ref<any[]>([])

  /**
   * BaseTable组件selection-change方法
   * @param val
   */
  const forSelectionChange = (val: any[]) => {
    curSelection.value = val
  }

  /**
   * BaseTable组件page-change方法
   */
  const forPageChange = () => {
    updateSelection(false)
  }

  /**
   * BaseTable组件data-change方法
   * @param val
   */
  const forDataChange = (val: any) => {
    tmpSelection.value = []
    // 回显已选择的内容
    const selectedRows = val.filter((item: any) => !!selection.value.find((ele: any) => item[key] === ele[key]))
    // console.log('selectedRows', selectedRows)
    selectedRows.forEach((row: any) => {
      tmpSelection.value.push(row)
      setTimeout(() => {
        dataTableRef.value.baseTableRef.toggleRowSelection(row, undefined)
      })
    })
  }

  /**
   * 备份各类型selection
   */
  const bacSelectionData = () => {
    selectionBac.value = [...selection.value]
    curSelectionBac.value = [...curSelection.value]
    tmpSelectionBac.value = [...tmpSelection.value]
  }

  /**
   * 恢复各类型selection
   */
  const recSelectrionData = () => {
    selection.value = [...selectionBac.value]
    curSelection.value = [...curSelectionBac.value]
    tmpSelection.value = [...tmpSelectionBac.value]
  }

  /**
   * 清空各类型selection
   */
  const clearSelectionData = () => {
    selection.value = []
    curSelection.value = []
    tmpSelection.value = []
  }

  /**
   * 更新selection
   * @returns {*} 全部选择项
   */
  const updateSelection = (rec = true) => {
    // 备份
    bacSelectionData()
    // 如果有暂存数据，则需要判断哪些是需要被删掉、哪些是需要被添加的项目
    const toDelete: any[] = []
    const toDeleteIds: any[] = []
    if (tmpSelection.value?.length) {
      tmpSelection.value.forEach((item: any) => {
        const target = curSelection.value.find((ele: any) => ele[key] === item[key])
        if (target) {
          const index = curSelection.value.indexOf(target)
          curSelection.value.splice(index, 1)
        } else {
          toDelete.push(item)
          toDeleteIds.push(item[key])
        }
      })
    }
    // console.log('toDelete', toDelete)
    // console.log('toAdd', curSelection.value)
    if (curSelection.value?.length) {
      // 新增内容
      curSelection.value.forEach((item: any) => {
        if (!selection.value.find((ele: any) => item[key] === ele[key])) {
          selection.value.push(item)
        }
      })
    }
    if (toDeleteIds?.length) {
      // 删除内容
      const tmp = selection.value.filter((item: any) => !toDeleteIds.includes(item[key]))
      selection.value = tmp
    }
    // 获得结果
    const result = [...selection.value]
    // 恢复现场
    if (rec) {
      recSelectrionData()
    }
    return result
  }

  return {
    forSelectionChange,
    forPageChange,
    forDataChange,
    updateSelection,
    clearSelectionData
  }
}
